#include "field.h"
#include <QMap>

QString Field::getId() const
{
    return id;
}

void Field::setId(const QString &newId)
{
    id = newId;
}

QString Field::getName() const
{
    return name;
}

void Field::setName(const QString &newName)
{
    name = newName;
}

double Field::getFactor() const
{
    return factor;
}

void Field::setFactor(double newFactor)
{
    factor = newFactor;
}

QString Field::getUnit() const
{
    return unit;
}

void Field::setUnit(const QString &newUnit)
{
    unit = newUnit;
}

int Field::getFieldWidth() const
{
    return fieldWidth;
}

void Field::setFieldWidth(int newFieldWidth)
{
    fieldWidth = newFieldWidth;
}

int Field::getPrecision() const
{
    return precision;
}

void Field::setPrecision(int newPrecision)
{
    precision = newPrecision;
}

double Field::getDispatchVal() const
{
    return dispatchVal;
}

void Field::setDispatchVal(double newDispatchVal)
{
    dispatchVal = newDispatchVal;
}

uint64_t Field::getHitVal() const
{
    return hitVal;
}

void Field::setHitVal(uint64_t newHitVal)
{
    hitVal = newHitVal;
}

void Field::setType(Type newType)
{
    type = newType;
}

namespace
{
using FieldType = Field::Type;
FieldType getByteFieldTypeByTag(const QString & tag)
{
    static const QMap<QString, Field::Type> str2type = {
        {"uint8",  Field::u8},
        {"uint16", Field::u16},
        {"uint32", Field::u32},
        {"uint64", Field::u64},
        {"int8",  Field::i8},
        {"int16", Field::i16},
        {"int32", Field::i32},
        {"int64", Field::i64},
        {"bcd",   Field::bcd}
        //        {"bit",   PField::boolean}
    };
    const QString integral = tag.simplified().toLower();
    return str2type.value(integral, Field::invalid);
}
}
bool Field::setType(const QString & str)
{
    const FieldType type = getByteFieldTypeByTag(str);
    this->setType(type);
    return (type != Field::invalid);
}

